package Z11_20_pool;
import lombok.extern.slf4j.Slf4j;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * @author： pang-yun
 * @date： 2021-11-20 14:34
 * 功能目的 ：用于   演示饥饿现象
 *
 * [pool-1-thread-1] INFO Z11_20_pool.Hungry - 处理点餐
 * [pool-1-thread-2] INFO Z11_20_pool.Hungry - 处理点餐
 * 两个线程 都卡在点餐  无法进行下一步
 */

@Slf4j
public class Hungry {
    static final List<String> MENU = Arrays.asList("红烧肉","凉拌黄瓜","肘子");
    static Random RANDOM = new Random();
    static String cooking(){return MENU.get(RANDOM.nextInt(MENU.size()));}

    public static void main(String[] args) {
        ExecutorService pool = Executors.newFixedThreadPool(2);

        pool.execute(()->{
            log.info("处理点餐");
            Future<String> cook = pool.submit(() -> {
                log.info("做菜");
                return cooking();
            });
            try {
                log.info("{}", cook.get());
            } catch (InterruptedException | ExecutionException e) {
                e.printStackTrace();
            }
        });

        pool.execute(()->{
            log.info("处理点餐");
            Future<String> cook = pool.submit(() -> {
                log.info("做菜");
                return cooking();
            });
            try {
                log.info("{}", cook.get());
            } catch (InterruptedException | ExecutionException e) {
                e.printStackTrace();
            }
        });
    }
}
